package com.pearadmin.edu.controller;

import com.github.pagehelper.PageInfo;
import com.pearadmin.edu.domain.Menu;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import com.pearadmin.common.tools.secure.SecurityUtil;
import com.pearadmin.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.pearadmin.edu.service.IMenuService;

import java.time.LocalDateTime;

/**
 * 前台管理Controller
 *
 * @author yanyi
 * @date 2021-07-04
 */
@RestController
@RequestMapping("/edu/menu")
public class MenuController extends BaseController
{
    private String prefix = "edu/menu";

    @Autowired
    private IMenuService menuService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/edu/menu/main','edu:menu:main')")
    public ModelAndView main()
    {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询前台管理列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/edu/menu/data','edu:menu:data')")
    public ResultTable list(@ModelAttribute Menu menu, PageDomain pageDomain)
    {
        PageInfo<Menu> pageInfo = menuService.selectMenuPage(menu,pageDomain);
        return pageTable(pageInfo.getList(),pageInfo.getTotal());
    }

    /**
     * 新增前台管理
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/edu/menu/add','edu:menu:add')")
    public ModelAndView add()
    {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增保存前台管理
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/edu/menu/add','edu:menu:add')")
    public Result save(@RequestBody Menu menu)
    {
        SysUser sysUser = (SysUser)SecurityUtil.currentUserObj();
        menu.setCreateTime(LocalDateTime.now());
        menu.setCreateBy(sysUser.getUserId());
        menu.setCreateName(sysUser.getUsername());
        return decide(menuService.insertMenu(menu));
    }

    /**
     * 修改前台管理
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/edu/menu/edit','edu:menu:edit')")
    public ModelAndView edit(Long id, ModelMap mmap)
    {
        Menu menu = menuService.selectMenuById(id);
        mmap.put("menu", menu);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改保存前台管理
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/edu/menu/edit','edu:menu:edit')")
    public Result update(@RequestBody Menu menu)
    {
        SysUser sysUser = (SysUser)SecurityUtil.currentUserObj();
        menu.setUpdateTime(LocalDateTime.now());
        menu.setUpdateBy(sysUser.getUserId());
        menu.setUpdateName(sysUser.getUsername());
        return decide(menuService.updateMenu(menu));
    }

    /**
     * 删除前台管理
     */
    @ResponseBody
    @DeleteMapping( "/batchRemove")
    @PreAuthorize("hasPermission('/edu/menu/remove','edu:menu:remove')")
    public Result batchRemove(String ids)
    {
        return decide(menuService.deleteMenuByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/edu/menu/remove','edu:menu:remove')")
    public Result remove(@PathVariable("id") Long id)
    {
        return decide(menuService.deleteMenuById(id));
    }
}
